﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppUpdates.Core
{
    public class FileCopy
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sources">原路径</param>
        /// <param name="dest">目标路径</param>
        /// <param name="overwrite"></param>
        public static void Copy(string sources, string dest, bool overwrite = true)
        {
            if (Directory.Exists(sources))
            {
                DirectoryInfo dinfo = new DirectoryInfo(sources);
                foreach (FileSystemInfo f in dinfo.GetFileSystemInfos())
                {
                    //  目標路徑destName = 目標文件夾路徑 + 原文件夾下的子路徑(或文件夾)名字  
                    String destName = Path.Combine(dest, f.Name);
                    if (f is FileInfo)//    如果是文件就复制                     
                    {
                        try
                        {
                            System.IO.File.Copy(f.FullName, destName, overwrite);//  true代表可以覆盖同名文件       
                        }
                        catch (Exception)
                        {
                        }
                    }
                    else//  如果是文件夾就創建文件夾然后复制然后递归复制                     
                    {
                        if (!Directory.Exists(destName))
                        {
                            Directory.CreateDirectory(destName);
                        }
                     
                        Copy(f.FullName, destName, overwrite);//   递归调用                     
                    }
                }
            }
        }
    }
}
